package com.zzy.lang.extend;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;

/**
 * 
 * @author guokaige
 *
 */
public final class ObjectUtils {

	/**
	 * return whether the given array is empty
	 */
	public static boolean isEmpty(Object[] array) {
		return (array == null || array.length == 0);
	}

	/**
	 * check if the given objects are equal
	 */
	public static boolean equals(Object o1, Object o2) {
		if (o1 == o2) {
			return true;
		}
		if (o1 == null || o2 == null) {
			return false;
		}
		if (o1.equals(o2)) {
			return true;
		}
		if (o1.getClass().isArray() && o2.getClass().isArray()) {
			if (o1 instanceof Object[] && o2 instanceof Object[]) {
				return Arrays.equals((Object[]) o1, (Object[]) o2);
			}
			if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
				return Arrays.equals((boolean[]) o1, (boolean[]) o2);
			}
			if (o1 instanceof byte[] && o2 instanceof byte[]) {
				return Arrays.equals((byte[]) o1, (byte[]) o2);
			}
			if (o1 instanceof char[] && o2 instanceof char[]) {
				return Arrays.equals((char[]) o1, (char[]) o2);
			}
			if (o1 instanceof double[] && o2 instanceof double[]) {
				return Arrays.equals((double[]) o1, (double[]) o2);
			}
			if (o1 instanceof float[] && o2 instanceof float[]) {
				return Arrays.equals((float[]) o1, (float[]) o2);
			}
			if (o1 instanceof int[] && o2 instanceof int[]) {
				return Arrays.equals((int[]) o1, (int[]) o2);
			}
			if (o1 instanceof long[] && o2 instanceof long[]) {
				return Arrays.equals((long[]) o1, (long[]) o2);
			}
			if (o1 instanceof short[] && o2 instanceof short[]) {
				return Arrays.equals((short[]) o1, (short[]) o2);
			}
		}
		return false;
	}

   /**
    * 数组添加
    * @Title:        title
    * @Description:  TODO(这里用一句话描述这个方法的作用) 
    * @param array
    * @param obj
    * @return    
    * @version       V3.0 
    * @author        guokaige@topnet.net.cn
    * @Date          2016年11月7日 下午1:04:24
    */
	public static Object[] addObjectToArray(Object[] array, Object obj) {
		Class<?> compType = Object.class;
		if (array != null) {
			compType = array.getClass().getComponentType();
		} else if (obj != null) {
			compType = obj.getClass();
		}
		int newArrLength = (array != null ? array.length + 1 : 1);
		Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength);
		if (array != null) {
			System.arraycopy(array, 0, newArr, 0, array.length);
		}
		newArr[newArr.length - 1] = obj;
		return newArr;
	}
	


	public static boolean isEmpty(Collection<?> collection){
		if(collection == null || collection.size() == 0 ){
			return true;
		}else{
			return false;
		}
	}
	
	
	public static Object formatData(Object obj){
		if(obj == null){
			return " - ";
		}
		if(obj instanceof String){
			return obj == null ? "-" : obj;
		}else if(obj instanceof Long){
			return obj == null ? "0" : obj;
		}else if(obj instanceof Integer){
			return obj == null ? "0" : obj;
		}else if(obj instanceof Double){
			return obj == null ? "0.0" : obj;
		}else if(obj instanceof Float){
			return obj == null ? "0.0" : obj;
		}else if(obj instanceof IEnumCode){
			return obj == null ? "-" : ((IEnumCode)obj).getCode();
		}else {
			return obj == null ? "-" : obj;
		}
	}
	
}
